/**
 * @package_name cn.knet.mb2b.android.utils
 */
package net.yangying.androidframework.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;


/**
 * @author YANGYING
 *
 * @date 2011-7-2
 *
 */
public class ZipUtils {

	public static boolean unzip(InputStream zipFileName, String outputDirectory) {
		boolean flag = false;
        try {
            ZipInputStream in = new ZipInputStream(zipFileName);
            // 获取ZipInputStream中的ZipEntry条目，一个zip文件中可能包含多个ZipEntry，
            // 当getNextEntry方法的返回值为null，则代表ZipInputStream中没有下一个ZipEntry，
            // 输入流读取完成；
            ZipEntry entry = in.getNextEntry();
            while (entry != null) {
                // 创建以zip包文件名为目录名的根目录
                File file = new File(outputDirectory);
                file.mkdirs();
                if (entry.isDirectory()) {
                    String name = entry.getName();
                    name = name.substring(0, name.length() - 1);
                    file = new File(outputDirectory + File.separator + name);
                    file.mkdirs();
                } else {
                    file = new File(outputDirectory + File.separator + entry.getName());
                    file.createNewFile();
                    int size;
                    byte[] buffer = new byte[20480];
                    FileOutputStream outStream = new FileOutputStream(file);
                    BufferedOutputStream bufferOut = new BufferedOutputStream(outStream, buffer.length);
                    while((size = in.read(buffer, 0, buffer.length)) != -1) {
                        bufferOut.write(buffer, 0, size);
                    }
                    bufferOut.flush();
                    bufferOut.close();
                }
                // 读取下一个ZipEntry
                entry = in.getNextEntry();
            }
            in.close();
            flag = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }
	public static boolean unzip(InputStream zipFileName, String outputDirectory, boolean forceCopy) {
		boolean flag = false;
        try {
            ZipInputStream in = new ZipInputStream(zipFileName);
            // 获取ZipInputStream中的ZipEntry条目，一个zip文件中可能包含多个ZipEntry，
            // 当getNextEntry方法的返回值为null，则代表ZipInputStream中没有下一个ZipEntry，
            // 输入流读取完成；
            ZipEntry entry = in.getNextEntry();
            while (entry != null) {
                // 创建以zip包文件名为目录名的根目录
                File file = new File(outputDirectory);
                file.mkdirs();
                if (entry.isDirectory()) {
                    String name = entry.getName();
                    name = name.substring(0, name.length() - 1);
                    file = new File(outputDirectory + File.separator + name);
                    file.mkdirs();
                } else {
                    file = new File(outputDirectory + File.separator + entry.getName());
                    if(!file.exists() || forceCopy){
                    	if(!file.exists()){
                    		Log.d("","CACHE NOT EXISTS, RECOPY");
                    	}
                    	file.createNewFile();
                        int size;
                        byte[] buffer = new byte[20480];
                        FileOutputStream outStream = new FileOutputStream(file);
                        BufferedOutputStream bufferOut = new BufferedOutputStream(outStream, buffer.length);
                        while((size = in.read(buffer, 0, buffer.length)) != -1) {
                            bufferOut.write(buffer, 0, size);
                        }
                        bufferOut.flush();
                        bufferOut.close();	
                    } else {
//                    	Log.d("","EXIST:"+file);
                    }
                }
                // 读取下一个ZipEntry
                entry = in.getNextEntry();
            }
            in.close();
            flag = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

}
